app.controller('ProcessModelListCtrl', ['$scope','$state','$rootScope', function($scope,$state,$rootScope) {
	var tree;
	$scope.processModeFolderTreeData;
	$scope.totalCount = 0;
	requestServlet("getProcessModeFolderTree",{},function(rs){
		$scope.processModeFolderTreeData = rs.list;
	},function(msg){});
	$scope.processModeFolderTree = tree = {};
	$scope.processModeFolderHandlerBranch=null;
	$scope.processModeFolderHandler = function(branch) {
		$scope.favoriteProcessModel = false;
		if($scope.processModeFolderHandlerBranch==branch){
			$scope.processModeFolderHandlerBranch.selected=false;
			$scope.processModeFolderHandlerBranch = null;
		}else{
			$scope.processModeFolderHandlerBranch=branch;
		}
		$scope.getProcessModelByFolder();
	};
	$scope.processModels;
	$scope.getProcessModelByFolder = function() {
		var data = {};
		if($scope.processModeFolderHandlerBranch!=null){
			data = {id:$scope.processModeFolderHandlerBranch.id}
		}
		requestServlet("getProcessModelByFolder", data, function (rs) {
			$scope.processModels = rs.list;
			$scope.totalCount = rs.totalCount;
		}, function (msg) {
		});
	}
	$scope.getProcessModelByFolder();

	$scope.searchProcessModel= function(e){
		$scope.favoriteProcessModel = false;
		if(e==null){
			if($scope.processModeFolderHandlerBranch!=null){
				$scope.processModeFolderHandlerBranch.selected=false;
				$scope.processModeFolderHandlerBranch = null;
			}
			requestServlet("getProcessModelByFolder", {}, function (rs) {
				var processModels=[];
				for(var i=0;i<rs.list.length;i++){
					if(rs.list[i].name.indexOf($scope.searchValue)>-1){
						processModels.push(rs.list[i]);
					}
				}
				$scope.processModels = processModels;
				$scope.totalCount = processModels.length;
			}, function (msg) {
			});
		}else{
			var keycode = window.event?e.keyCode:e.which;
			if(keycode==13){
				if($scope.processModeFolderHandlerBranch!=null){
					$scope.processModeFolderHandlerBranch.selected=false;
					$scope.processModeFolderHandlerBranch = null;
				}
				requestServlet("getProcessModelByFolder", {}, function (rs) {
					var processModels=[];
					for(var i=0;i<rs.list.length;i++){
						if(rs.list[i].name.indexOf($scope.searchValue)>-1){
							processModels.push(rs.list[i]);
						}
					}
					$scope.processModels = processModels;
					$scope.totalCount = processModels.length;
				}, function (msg) {});
			}
		}
	}
	$scope.favoriteProcessModel = false;
	$scope.favoriteProcessModelFn  = function(){
		if($scope.processModeFolderHandlerBranch!=null){
			$scope.processModeFolderHandlerBranch.selected=false;
			$scope.processModeFolderHandlerBranch = null;
		}
		if($scope.favoriteProcessModel){
			$scope.favoriteProcessModel = false;
			$scope.getProcessModelByFolder();
		}else {
			$scope.favoriteProcessModel = true;
			requestServlet("getProcessModelByFolder", {}, function (rs) {
				var processModels=[];
				for(var i=0;i<rs.list.length;i++){
					if(rs.list[i].favorite){
						processModels.push(rs.list[i]);
					}
				}
				$scope.processModels = processModels;
				$scope.totalCount = processModels.length;
			}, function (msg) {
			});
		}

	}

	//收藏切换
	$scope.setFavorite = function(id,favorite,index,e){
		if(favorite){
			favorite = 1;
		}else{
			favorite = 0;
		}
		requestServlet("setProcessModelFavorite",{is_favorite:favorite,id:id},function(rs){
			if(favorite==1){
				$scope.processModels[index]["favorite"] = false;
			}else{
				$scope.processModels[index]["favorite"] = true;
			}
		},function(msg){});
		e.stopPropagation();
	}

}]);
app.controller('ProcessModelStaertCtrl', ['$scope','$state','$stateParams', function($scope,$state,$stateParams) {
	$scope.pm;
	requestServlet("getProcessModelDetail", {id:$stateParams.processModelId}, function (rs) {
		$scope.pm = rs;
		if($stateParams.processParameter!=0) {
			$stateParams.processParameter = $stateParams.processParameter.replaceAll('slash','/');
			$("#processModelContent").attr("src", "/suite/app/processtasks/process_model_form.jsp?uuid=" + $scope.pm.uuid + $stateParams.processParameter)
		}else{
			$("#processModelContent").attr("src", "/suite/app/processtasks/process_model_form.jsp?uuid=" + $scope.pm.uuid)
		}
		window.addEventListener('message',function(e){
			var data = JSON.parse(e.data)
			if(data.type=="setHeight"){
				$("#processModelContent").height(data.height);
				$("#processModelContent").parent().removeClass('div-loading')
			}else if(data.type=="submit"){
				$state.go("app.processdetail",{processId:data.processDetail.id});
			}
		},false);
	}, function (msg) {});


}]);
app.controller('ProcessModelDetailCtrl', ['$scope','$state','$stateParams', function($scope,$state,$stateParams) {
	$("#content_view").css("background-color", "#fff");
	$scope.pm;
	$scope.wizardData
	requestServlet("getProcessModelDetail", {id:$stateParams.processModelId}, function (rs) {
		$scope.pm = rs;
		setTimeout(function(){
			$("#processModelDetailChart").attr("src", "/suite/app/processtasks/process_model_run_chart.jsp?id=" + $scope.pm.id);
		},0);
	}, function (msg) {});
	requestServlet("getProcessModelWizardData",{id:$stateParams.processModelId},function(rs){
		$scope.wizardData=rs;
	},function(msg){});
	//是否收藏
	$scope.isFavorite = function(favorite){
		if(favorite==1){
			return "active";
		}
	}
	//收藏切换
	$scope.setFavorite = function(id,favorite){
		if(favorite){
			favorite = 1;
		}else{
			favorite = 0;
		}
		requestServlet("setProcessModelFavorite",{is_favorite:favorite,id:id},function(rs){
		},function(msg){});
	}
}]);
app.controller('processModelDashboard', ['$scope','$state','$rootScope','$stateParams', function($scope,$state,$rootScope,$stateParams) {
	$("#content_view").css("background-color", "#fff");
	$scope.processModel;
	$scope.page;
	requestServlet("getProcessModelDetail", {id:$stateParams.processModelId}, function (rs) {
		$scope.processModel = rs;
		requestServlet("getPageInfo", {id:$scope.processModel.dashboardPageId}, function (rs) {
			$scope.page = rs;
		}, function (msg) {});
	}, function (msg) {});
	window.addEventListener('message',function(e){
		var data = JSON.parse(e.data)
		if(data.type=="removeLoading"){
			$("#report_"+data.reportId).removeClass('div-loading');
		}
	},false);

	$scope.loadReportIframe=function(portletId,reportId){
		var iframeObj = $("#"+portletId+reportId);
		if(iframeObj.attr("id")!=null){
			if(iframeObj.attr("src")==null){
				var graphSize = Number(iframeObj.attr("graphSize"));
				var height = Number(iframeObj.attr("height"));
				if(height==270||height==0){
					iframeObj.height($(window).height()*(graphSize/100));
				}else{
					iframeObj.height(height);
				}

				iframeObj.attr("src","/suite/app/processtasks/report_detail.jsp?id="+reportId);
			}
		}
	}
	$scope.loadPortlet=function(frame){
		if(frame=="portlet.titleonly"){
			return "panel panel-default";
		}else if(frame=="portlet.default"){
			return "panel panel-info";
		}else if(frame=="portlet.titleframe"){
			return "panel panel-success";
		}else if(frame=="portlet.plain"){
			return "panel panel-default";
		}
	}
	$scope.loadStarkmarksClass=function(inBookmarks){
		if(inBookmarks){
			return "fa fa-star text-muted text-active";
		}else{
			return "fa fa-star text-muted text";
		}
	}
	$scope.loadStarOBookmarksClass=function(inBookmarks){
		if(inBookmarks){
			return "fa fa-star text-warning text";
		}else{
			return "fa fa-star text-warning text-active";
		}
	}
	$scope.toggleBookmarks=function(id,inBookmarks){
		if(inBookmarks){
			requestServlet("removeBookmarks",{id:id},function(rs){
				$state.reload();
			},function(msg){});
		}else{
			requestServlet("addBookmarks",{id:id},function(rs){
				$state.reload();
			},function(msg){});

		}
	}
}]);